package com.parking.status.sync.netty.service;

import com.parking.status.sync.entity.*;
import com.parking.status.sync.netty.entity.RequestPre;
import com.parking.status.sync.netty.entity.ResponseParkingCheckin;
import com.parking.status.sync.netty.entity.ResponseParkingIn;
import com.parking.status.sync.netty.entity.ResponsePre;
import com.parking.status.sync.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by Administrator on 2015/9/25.
 */
@Component
public class CommandFactory {

    @Autowired
    CommandSyncParkingInfoRepository commandSyncParkingInfoRepository;
    @Autowired
    CommandParkingInRepository commandParkingInRepository;
    @Autowired
    CommandParkingOutRepository commandParkingOutRepository;
    @Autowired
    CommandParkingCheckinRepository commandParkingCheckinRepository;
    @Autowired
    CommandHeartBeatRepository commandHeartBeatRepository;

    public CommandBase createCommandFromDB() {
        CommandBase command = null;
        while(true) {
            List<CommandSyncParkingInfo> commandSyncParkingInfos = commandSyncParkingInfoRepository.findStatusEqualZero();
            if (commandSyncParkingInfos.size() >= 1) {
                command = commandSyncParkingInfos.get(0);
                break;
            }

            List<CommandParkingIn> commandParkingIns = commandParkingInRepository.findStatusEqualZero();
            if (commandParkingIns.size() >= 1) {
                command = commandParkingIns.get(0);
                break;
            }

            List<CommandParkingOut> commandParkingOuts = commandParkingOutRepository.findStatusEqualZero();
            if (commandParkingOuts.size() >= 1) {
                command = commandParkingOuts.get(0);
                break;
            }

            List<CommandParkingCheckin> commandParkingCheckins = commandParkingCheckinRepository.findStatusEqualZero();
            if (commandParkingCheckins.size() >= 1) {
                command = commandParkingCheckins.get(0);
                break;
            }

            List<CommandHeartBeat> commandHeartBeats = commandHeartBeatRepository.findStatusEqualZero();
            if (commandHeartBeats.size() >= 1) {
                command = commandHeartBeats.get(0);
                break;
            }

            break;
        }
        return command;
    }

    public CommandBaseRepository getCommandRepository(Class<? extends ResponsePre> responseClass) {
        if (responseClass == ResponseParkingCheckin.class) {
            return commandParkingCheckinRepository;
        } else if(responseClass == ResponseParkingIn.class) {
            //return commandParkingInRepository;
        } else {}
        return null;
    }

}
